package com.zoffy.bi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.zoffy.bi.annotation.AuthCheck;
import com.zoffy.bi.common.BaseResponse;
import com.zoffy.bi.common.DeleteRequest;
import com.zoffy.bi.common.ErrorCode;
import com.zoffy.bi.common.ResultUtils;
import com.zoffy.bi.constant.CommonConstant;
import com.zoffy.bi.constant.UserConstant;
import com.zoffy.bi.exception.BusinessException;
import com.zoffy.bi.exception.ThrowUtils;
import com.zoffy.bi.model.dto.chart.*;
import com.zoffy.bi.model.entity.Chart;
import com.zoffy.bi.model.entity.User;
import com.zoffy.bi.model.vo.BiResponse;
import com.zoffy.bi.service.ChartService;
import com.zoffy.bi.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.zoffy.bi.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 帖子接口
 *
 * @author <a href="https://gitee.com/zoffyshine">zoffy</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@RestController
@RequestMapping("/chart")
@Slf4j
public class ChartController {

    @Resource
    private ChartService chartService;

    @Resource
    private UserService userService;


    /**
     * 智能分析
     *
     * @param multipartFile 上传文件
     * @param genChartByAiRequest 用户参数
     * @param request HttpServletRequesr
     * @return BaseResponse
     */
    @PostMapping("/gen")
    public  BaseResponse<BiResponse>  genChartByAi(@RequestPart("file") MultipartFile multipartFile,
                                                   GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) {
        return chartService.genChartByAi(multipartFile, genChartByAiRequest, request);
    }
    @PostMapping("/gen/async")
    public  BaseResponse<BiResponse>  genChartByAiAsync(@RequestPart("file") MultipartFile multipartFile,
                                                   GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) {
        return chartService.genChartByAiAsync(multipartFile, genChartByAiRequest, request);
    }
    @PostMapping("/gen/mq")
    public  BaseResponse<BiResponse>  genChartByAiMq(@RequestPart("file") MultipartFile multipartFile,
                                                        GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) {
        return chartService.genChartByAiMq(multipartFile, genChartByAiRequest, request);
    }

    // region 增删改查

    /**
     * 分页获取当前用户创建的资源列表
     *
     * @param chartQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<Chart>> listMyChartByPage(@RequestBody ChartQueryRequest chartQueryRequest,
            HttpServletRequest request) {
        if (chartQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        chartQueryRequest.setUserId(loginUser.getId());
        long current = chartQueryRequest.getPage();
        long size = chartQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Chart> chartPage = chartService.page(new Page<>(current, size),
                getQueryWrapper(chartQueryRequest));
        return ResultUtils.success(chartPage);
    }

    // endregion


    public QueryWrapper<Chart> getQueryWrapper(ChartQueryRequest chartQueryRequest) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        if (chartQueryRequest == null) {
            return queryWrapper;
        }
        Long userId = chartQueryRequest.getUserId();
        String goal = chartQueryRequest.getGoal();
        String chartType = chartQueryRequest.getChartType();
        String sortField = chartQueryRequest.getSortField();
        String sortOrder = chartQueryRequest.getSortOrder();
        String name=chartQueryRequest.getName();


        queryWrapper.eq(userId>0,"user_id",userId);
        queryWrapper.eq(StringUtils.isNotBlank(goal),"goal",goal);
        queryWrapper.eq(StringUtils.isNotBlank(chartType),"chartType",chartType);
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}
